
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import utils.UStrings;
import webservice.*;
import javax.xml.ws.Endpoint;


public class MegaWarsServer {
	
	private static String adress = "http://localhost";
	private static String port = "8888";
	private static String webServiceName = "MegaWarsWs";
	
	private MegaWarsWS impl;
	private Endpoint endpoint;
	
	
	public MegaWarsServer () {
		impl = new MegaWarsWS();
	}
	
	
	/**
	 * Start publishing WS
	 * return true if can be published, false otherwise
	 */
	public boolean start () {
		System.out.println("Starting publishing WS on " + getWSAdress());
		if(endpoint==null) {
			endpoint = Endpoint.create(impl);
		}
		
		if(endpoint.isPublished()) {
			System.out.println("Warning WS is already published");
			return false;
		}
		
		endpoint.publish(getWSAdress());
		
		if(endpoint.isPublished()) {
			System.out.println("Ok, WS is published correctly");
		}
		else {
			System.err.println("WS could not be published");
		}
		
		return endpoint.isPublished();
	}
	
	/**
	 * Stop publishing WS
	 * Do nothing if not started
	 */
	public void stop() {
		System.out.println("Begin to stop WS");
		if(endpoint!=null) {
			endpoint.stop();
			endpoint = null;
		}
		System.out.println("WS is now stopped");
	}
	
	
	/**
	 * Restart publishing the host
	 * return true if restart ok, false otherwise
	 */
	public boolean restart () {
		stop();
		return start();
	}
	
	
	
	public boolean createUser(String name, String password, String email) {
		if(UStrings.isValidName(name) && UStrings.isValidEmail(email)) {
			
		}
		else {
			
		}
		
		return false;
	}
	
	
	
	
	
	
	/**
	 * @return return the adress to publis the WS
	 */
	public String getWSAdress() {
		return adress + ":" + port + "/" + webServiceName;
	}
	
	/**
	 * Give an order to the host
	 * 
	 * @param line [start] | [stop] | [restart] | [port portnumber] | [adress url] | [wsname webservicename] | [quit]
	 * @return return true if quit order is given, false otherwise
	 */
	public boolean giveOrder(String line) {
		String [] cmdArgs = line.split("\\s+");
		for(int i=0; i<cmdArgs.length; i++){
			switch (cmdArgs[i]) {
			
				case "start" : start(); break;
				case "restart" : restart(); break;
				case "stop" : stop(); break;
				case "exit":
				case "quit" : return true;
				case "help" : printHelp(); break;
				case "port" :
					i++;
					if(i<cmdArgs.length)
						port = cmdArgs[i];
					break;
				case "adress" :
					i++;
					if(i<cmdArgs.length)
						adress = cmdArgs[i];
					break;
				case "wsname" :
					i++;
					if(i<cmdArgs.length)
						webServiceName = cmdArgs[i];
					break;
				default:
					System.out.println("Unknown command "+ cmdArgs[i] + cmdArgs.length + "\n" + line);
					break;
			}
		}
		
		return false;
	}

	/**
	 * Say quit or exit to finish application
	 * @param args
	 */
	public static void main(String[] args) {
		

		BufferedReader readerIn = new BufferedReader(new InputStreamReader(System.in));
		MegaWarsServer server = new MegaWarsServer();
		server.printHelp ();
		//readerIn = new BufferedReader(new InputStreamReader(System.in));
		
		
		
		/*MegaWarsWS impl = new MegaWarsWS();
		
		Endpoint endpoint = Endpoint.publish(URI, impl);
		
		boolean status = endpoint.isPublished();
		
		System.out.println("WS is published " + status);
		
		*/
		
		//read command line args
		String line;
		boolean quit = false;
		while (!quit) {
			
			try {
				if((line = readerIn.readLine()) !=null ) {
					quit = server.giveOrder(line);
				}
				else {
					System.out.println("could not read anything");
				}
				
			}
			catch (IOException e) {
				e.printStackTrace();
				quit = true;
			}
		}
		
		
		
		//release and quit
		server.stop();
		try {
			readerIn.close();
		} catch (IOException e) { }
		
		System.out.println("Server shut down \n\n");
		

	}
	
	
	
	public void printHelp () {
		System.out.println("Welcome to Megawars host \n" +
			"Host will be published on adress " + getWSAdress() + "\n\n" +
			"Usage : \n" +
			"[start] start publish host\n" +
			"| [stop] stop publishing host\n" +
			"| [restart] restart publishing host\n" +
			"| [port portnumber] change port number to publish the host (must restart)\n" +
			"| [adress url] change url to publish the host (must restart)\n" +
			"| [wsname webservicename] change ws name to publish the host (must restart)\n" +
			"| [help] print this screen\n" +
			"| [quit] exit program\n\n"
			);
	}
	
	
	
	

}
